package com.spring.util;


import org.apache.http.HttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨紫添
 * @description
 * @create 2022-04-26-17:16
 */
public class SendShortMessage {

    public static String sendShortMessage(String phone_number,String message) {
        String host = "https://dfsns.market.alicloudapi.com";
        String path = "/data/send_sms";
        String method = "POST";
        String appcode = "57a813c4ceea4f18ab5731f0fb457be5";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();

        StringBuilder builder = new StringBuilder();
//        for (int i = 0; i < 6; i++) {
//            int num = (int) (Math.random() * 10);
//            builder.append(num);
//        }
        String code = builder.toString();
        bodys.put("content", "code:"+message+",expire_at:5");
        bodys.put("phone_number", phone_number);
        bodys.put("template_id", "TPL_0001");

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            int statusCode = response.getStatusLine().getStatusCode();
            String reasonPhrase = response.getStatusLine().getReasonPhrase();
            if (statusCode == 200) {
                return "发送成功";
            }
            return "发送失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败";
        }
    }
}
